package cn.shone.hubu.dao;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import cn.shone.hubu.common.HibernatePage;
import cn.shone.hubu.po.HbNeed;

/**
 * Data access object (DAO) for domain model class HbNeed.
 * 
 * @see cn.shone.hubu.po.HbNeed
 * @author MyEclipse Persistence Tools
 */

public class HbNeedDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(HbNeedDAO.class);

	// property constants
	public static final String NEEDNO = "needno";

	public static final String TITLE = "title";

	public static final String CONTENT = "content";

	public static final String DAYS = "days";

	public static final String DISTRICT = "district";

	public static final String CITY = "city";

	public static final String NEEDTYPE = "needtype";

	public static final String LINKPHONE = "linkphone";

	public static final String LINKQQ = "linkqq";

	public static final String PUBTYPE = "pubtype";

	public static final String PROMISE = "promise";

	public static final String FEE = "fee";

	public static final String FEETYPE = "feetype";

	public static final String FPRICE = "fprice";

	public static final String SPRICE = "sprice";

	public static final String TPRICE = "tprice";

	public static final String NUM = "num";

	public static final String NPRICE = "nprice";

	public static final String COST = "cost";

	public static final String TJFLAG1 = "tjflag1";

	public static final String TJFLAG2 = "tjflag2";

	public static final String TJFLAG3 = "tjflag3";

	public static final String TJFLAG4 = "tjflag4";

	public static final String TJFLAG5 = "tjflag5";

	public static final String FEESUM = "feesum";

	public static final String CREPLY = "creply";

	public static final String CVIEW = "cview";

	public static final String CDELAY = "cdelay";

	public static final String NEEDSTATUS = "needstatus";

	public static final String FEESTATUS = "feestatus";

	public static final String CREATER = "creater";

	public static final String EDITID = "editid";

	public static final String STATUS = "status";

	public static final String CHECKID = "checkid";

	public void save(HbNeed transientInstance) {
		log.debug("saving HbNeed instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(HbNeed persistentInstance) {
		log.debug("deleting HbNeed instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public HbNeed findById(java.lang.Integer id) {
		log.debug("getting HbNeed instance with id: " + id);
		try {
			HbNeed instance = (HbNeed) getSession().get(
					"cn.shone.hubu.po.HbNeed", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(HbNeed instance) {
		log.debug("finding HbNeed instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.shone.hubu.po.HbNeed").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding HbNeed instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from HbNeed as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByNeedno(Object needno) {
		return findByProperty(NEEDNO, needno);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByDays(Object days) {
		return findByProperty(DAYS, days);
	}

	public List findByDistrict(Object district) {
		return findByProperty(DISTRICT, district);
	}

	public List findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	public List findByNeedtype(Object needtype) {
		return findByProperty(NEEDTYPE, needtype);
	}

	public List findByLinkphone(Object linkphone) {
		return findByProperty(LINKPHONE, linkphone);
	}

	public List findByLinkqq(Object linkqq) {
		return findByProperty(LINKQQ, linkqq);
	}

	public List findByPubtype(Object pubtype) {
		return findByProperty(PUBTYPE, pubtype);
	}

	public List findByPromise(Object promise) {
		return findByProperty(PROMISE, promise);
	}

	public List findByFee(Object fee) {
		return findByProperty(FEE, fee);
	}

	public List findByFeetype(Object feetype) {
		return findByProperty(FEETYPE, feetype);
	}

	public List findByFprice(Object fprice) {
		return findByProperty(FPRICE, fprice);
	}

	public List findBySprice(Object sprice) {
		return findByProperty(SPRICE, sprice);
	}

	public List findByTprice(Object tprice) {
		return findByProperty(TPRICE, tprice);
	}

	public List findByNum(Object num) {
		return findByProperty(NUM, num);
	}

	public List findByNprice(Object nprice) {
		return findByProperty(NPRICE, nprice);
	}

	public List findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	public List findByTjflag1(Object tjflag1) {
		return findByProperty(TJFLAG1, tjflag1);
	}

	public List findByTjflag2(Object tjflag2) {
		return findByProperty(TJFLAG2, tjflag2);
	}

	public List findByTjflag3(Object tjflag3) {
		return findByProperty(TJFLAG3, tjflag3);
	}

	public List findByTjflag4(Object tjflag4) {
		return findByProperty(TJFLAG4, tjflag4);
	}

	public List findByTjflag5(Object tjflag5) {
		return findByProperty(TJFLAG5, tjflag5);
	}

	public List findByFeesum(Object feesum) {
		return findByProperty(FEESUM, feesum);
	}

	public List findByCreply(Object creply) {
		return findByProperty(CREPLY, creply);
	}

	public List findByCview(Object cview) {
		return findByProperty(CVIEW, cview);
	}

	public List findByCdelay(Object cdelay) {
		return findByProperty(CDELAY, cdelay);
	}

	public List findByNeedstatus(Object needstatus) {
		return findByProperty(NEEDSTATUS, needstatus);
	}

	public List findByFeestatus(Object feestatus) {
		return findByProperty(FEESTATUS, feestatus);
	}

	public List findByCreater(Object creater) {
		return findByProperty(CREATER, creater);
	}

	public List findByEditid(Object editid) {
		return findByProperty(EDITID, editid);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByCheckid(Object checkid) {
		return findByProperty(CHECKID, checkid);
	}

	public List findAll() {
		log.debug("finding all HbNeed instances");
		try {
			String queryString = "from HbNeed";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public HbNeed merge(HbNeed detachedInstance) {
		log.debug("merging HbNeed instance");
		try {
			HbNeed result = (HbNeed) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(HbNeed instance) {
		log.debug("attaching dirty HbNeed instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(HbNeed instance) {
		log.debug("attaching clean HbNeed instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public HibernatePage findByExample(JSONObject json){
		return HibernatePage.findByExample(getSession(), "HbNeed", json, 0, 1);
	}
	
	public static void main(String args[]){
		HbNeedDAO hnd = new HbNeedDAO();
//		Transaction tx = hnd.getSession().beginTransaction();
//		HbNeed hn = new HbNeed();
//		hn.setNeedno(12);
//		hnd.save(hn);
//		tx.commit();
		JSONObject json = JSONObject.fromObject("{needno:12}");
		
		HibernatePage hp = hnd.findByExample(json);
		System.out.println(hp.getTotal());
	}
}